// 网关配置的前缀
var companyServer = "/companySer"

// let companyId = "1354255569179971586";
let companyId = "";

let Authorization = "";
let refreshToken = "";
let userRole = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userRole = $("#role").html();
    companyId = $("#companyId").html();
    getMemberInfo();
}

function getMemberInfo() {
    $.ajax({
        type: "GET",
        async: false,
        url: companyServer + "/company/memberInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {

            //清空数据
            $("#memberList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                let buyMemberButton = ""
                if (userRole === "company"){
                    buyMemberButton = "<a class='ht-btn transparent-btn-two' onclick='buyMember(\"" + item.typeName + "\")'>现在开始 </a>";
                }

                $("#memberList").append(
                    "<div class='col-lg-3 col-md-6 col-sm-6'>" +
                    "<div class='pricing-wrap mb-30'>" +
                    "<div class='single-pricing'>" +
                    "<div class='item-header'>" +
                    "<h2 class='title'>" + item.typeName + "</h2>" +
                    "</div>" +
                    "<div class='price-table-tag'>" +
                    "<div class='price-tag currency-placement-left'>" +
                    "<div class='pricing-price-amount'>" +
                    "<span class='price-currency currency'>$ </span>" +
                    "<span class='price-amount'>" + item.price + "</span>" +
                    "</div>" +
                    "</div>" +
                    "<span class='price-period'>每月一次 </span>" +
                    "</div>" +
                    "<div class='price-table-feature d-flex flex-column'>" +
                    "<ul class='list-box-feature'>" +
                    "<li class='item-list-feature'>" +
                    "<i class='fa fa-check'></i>" +
                    "<span><b>" + item.postNumber + "</b> 个职位发布</span>" +
                    "</li>" +
                    "<li class='item-list-feature'>" +
                    "<i class='fa fa-check'></i>" +
                    "<span><b>" + item.hrNumber + "</b> 个 HR 账号</span>" +
                    "</li>" +
                    "</ul>" +
                    "</div>" +
                    "<div class='item_button'>" +
                    buyMemberButton+
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>"
                )

            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

//
//this.setContentPrepend('<div>Prepended text</div>');
function buyMember(type) {
    $.confirm({
        title: '提示',
        content: "确认购买 [ " + type + " ] 的会员吗？",
        onContentReady: function () {
            this.setContentPrepend("<img style='height: 140px;width: 140px' src='/images/zhifu.png'/>");
        },
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '支付完成',
                btnClass: 'btn-warning',
                action: function () {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: companyServer + "/company/companyInfo",
                        headers: {
                            "Content-Type": "application/json",
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        data: JSON.stringify({
                            "companyId": companyId,
                            "memberLevel": type,
                        }),
                        success: function (data) {
                            if (data.resp_code === 0) {

                                // 成功后将订单信息加入数据库
                                $.ajax({
                                    type: "PUT",
                                    async: false,
                                    url: companyServer + "/company/orderInfo",
                                    headers: {
                                        "Content-Type": "application/json",
                                        "Authorization": Authorization,
                                        "refreshToken": refreshToken,
                                    },
                                    data: JSON.stringify({
                                        "companyId": companyId,
                                        "memberLevel": type,
                                        "term": 1,
                                    }),
                                    success: function (data) {
                                        if (data.resp_code === 0) {
                                            cocoMessage.info(2000, "会员购买成功", function () {
                                            });
                                        } else {
                                            console.log(data.resp_msg);
                                        }
                                    },
                                    error: function (err) {
                                        // 异常
                                        console.log(err);
                                    }
                                })

                            } else {
                                // 失败
                                cocoMessage.error(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                            }
                        },
                        error: function (err) {
                            // 异常
                            cocoMessage.error(2000, err.statusText, function () {
                                console.log(err);
                            });
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}
